import { MyChroma } from './myChroma.js'
import {myEmbeddings} from '../embeddings/index.js'
import { chromaClient } from './client.js'

const collections = {}

const collections_info = await chromaClient.listCollections()

collections_info.forEach(info=>{
    collections[info.name] = new MyChroma(myEmbeddings, {
        collectionName: info.name,
        index: chromaClient
    })
    collections[info.name].ensureCollection()
})

export const addCollection = async (collectionName)=>{
    if(!Object.keys(collections).includes(collectionName)){
        collections[collectionName] = new MyChroma(myEmbeddings, {
            collectionName: collectionName,
            index: chromaClient
        })
        await collections[collectionName].ensureCollection()
    }
    return collections[collectionName]
}

export const getCollection = (collectionName)=>{
    return collections[collectionName]
}

export const deleteCollection = async (collectionName)=>{
    return new Promise((resolve, reject) => {
        collections[collectionName].deleteCollection()
            .then(() => {
                delete collections[collectionName];
                resolve('success');
            })
            .catch((error) => {
                reject(error);
            });
    });
}

export const getAllCollections = ()=>{
    return Object.keys(collections)
}
